package br.com.felix.easyclearcase.ui.dialogs;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import br.com.felix.clearcase.command.CommandOutput;
import br.com.felix.clearcase.command.CommandTarget;
import br.com.felix.clearcase.command.ParametrizedCommand;



/**
 * Dialog used to create a new command.
 * */
public class NewCommandDialog extends JDialog {

	private static final long serialVersionUID = -712571235283839879L;
	
	private JButton btnSave = new JButton("save");
	
	/**
	 * Will contain the command name.
	 * */
	private JTextField txtCommandName = new JTextField();
	
	/**
	 * Will contain the command text, for example: "cleartool ls".
	 * */
	private JTextField txtCommandText = new JTextField();
	
	/**
	 * Will contain the command output type.
	 * @see CommandOutput
	 * */
	private JComboBox cboOutput = new JComboBox();
	
	/**
	 * Will contain the command target.
	 * @see CommandTarget
	 * */
	private JComboBox cboApplyTo = new JComboBox();
	
	private JTextArea txtaInstructions = new JTextArea("To create a customized command, specify a valid ClearTool command.\nYou may add parameters using the \"dolar{variablename}\" syntax.\nFor example: ${label}.\nTo retrieve the selected file path, use the ${file} variable.");
	
	/**
	 * Constructor initializes UI.
	 * */
	public NewCommandDialog(){
		setTitle("New Command");
		setSize(new Dimension(600,240));
		
		setLayout(new GridBagLayout());
		GridBagConstraints gbc = new GridBagConstraints();

		gbc.anchor = GridBagConstraints.NORTHWEST;
		gbc.insets = new Insets(0,0,15,15);
		gbc.gridx = 0;
		gbc.gridy = 0;
		
		txtaInstructions.setBorder(null);
		txtaInstructions.setBackground(getBackground());
		gbc.gridwidth=2;
		add(txtaInstructions,gbc);
		
		gbc.insets = new Insets(0,0,0,15);
		gbc.gridwidth=1;
		gbc.gridy++;
		JLabel lblCommandName = new JLabel("Command name:");
		add(lblCommandName,gbc);
		
		gbc.gridx++;
		txtCommandName.setColumns(15);
		add(txtCommandName,gbc);
		
		gbc.gridx=0;
		gbc.gridy++;
		JLabel lblCommandText = new JLabel("ClearTool command:");
		add(lblCommandText,gbc);
		
		gbc.gridx++;
		txtCommandText.setColumns(30);
		add(txtCommandText,gbc);
		
		
		gbc.gridx=0;
		gbc.gridy++;
		JLabel lblApplyTo = new JLabel("Apply to:");
		add(lblApplyTo,gbc);
		
		
		for(CommandTarget tgt: CommandTarget.values()){
			cboApplyTo.addItem(tgt);
		}
		gbc.gridx++;
		add(cboApplyTo,gbc);
		
		gbc.gridx=0;
		gbc.gridy++;
		JLabel lblOutput = new JLabel("Command output:");
		add(lblOutput,gbc);
		
		
		for(CommandOutput output: CommandOutput.values()){
			cboOutput.addItem(output);
		}
		gbc.gridx++;
		add(cboOutput,gbc);
		
		
		JButton btnCancel = new JButton("cancel");

		MouseAdapter disposeMouseAdapter = new MouseAdapter(){
					@Override
					public void mouseReleased(MouseEvent e) {
						dispose();
					}
				};
		btnCancel.addMouseListener(disposeMouseAdapter);
		
		JPanel panelBotoes = new JPanel();
		panelBotoes.add(btnSave);
		panelBotoes.add(btnCancel);
		
		gbc.gridx = 1;
		gbc.gridy++;
		
		add(panelBotoes,gbc);
	}
	
	
	/**
	 * Returns the ParametrizedCommand comprising the user inputted information.
	 * */
	public ParametrizedCommand getParametrizedCommand(){
		String command = txtCommandText.getText();
		ParametrizedCommand cmd = new ParametrizedCommand(command);
		cmd.setOutput((CommandOutput)cboOutput.getSelectedItem());
		cmd.setTarget((CommandTarget)cboApplyTo.getSelectedItem());
		cmd.setName(txtCommandName.getText());
		return cmd;
	}
	
	
	public static void main(String args[]){
		NewCommandDialog ncd = new NewCommandDialog();
		ncd.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		ncd.setVisible(true);
	}


	@Override
	public void setVisible(boolean b) {
		txtaInstructions.setBackground(getBackground());
		super.setVisible(b);
	}
	
	
	/**
	 * Adds a mouse listener to the add button.
	 * */
	public void addSaveMouseListener(MouseListener ml){
		btnSave.addMouseListener(ml);
	}
}
